package com.louis.contact;

import com.louis.R;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.RawContactsEntity;
//import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Data;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class ContactListActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
//		final Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[]{Data._ID,Data.DISPLAY_NAME}, Data.MIMETYPE + "='" + CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE+ "'", null,Data.SORT_KEY_PRIMARY+ " ASC");
		final Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME}, null, null,ContactsContract.Contacts.SORT_KEY_PRIMARY+ " ASC");
		Log.i("Contact",cursor.getCount()+"");
		//		Cursor c = getContentResolver().query(Data.CONTENT_URI,
//		          new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
//		          Data.RAW_CONTACT_ID + "=?" + " AND "
//		                  + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
//		         null, null);
//		Cursor cursor = this.managedQuery(People.CONTENT_URI, new String[]{People._ID}, null, null, People._ID + " ASC");
		this.setContentView(R.layout.contactlistactivity);
		ListView lv = (ListView)findViewById(R.id.listView1);
//		new String[]{Data._ID},new int[]{android.R.id.text1}
		CursorAdapter ca = new SimpleCursorAdapter(ContactListActivity.this,android.R.layout.simple_list_item_1,cursor,new String[]{ContactsContract.Contacts.DISPLAY_NAME},new int[]{android.R.id.text1});
		lv.setAdapter(ca);
		
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				cursor.moveToPosition(arg2);
				Intent intent = new Intent(ContactListActivity.this,ContactDetailActivity.class);
				intent.putExtra("Data_id", cursor.getString(0));
				startActivity(intent);
			}
			
		});
	}
	
	
}
